package com.sky.service.impl;

import com.github.pagehelper.Constant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    private static final Logger log = LoggerFactory.getLogger(DishServiceImpl.class);
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * 
 * @param dishDTO
     * @return 
     * @author heiheiheng
     * @create 2025/7/18
     **/
    @Override
    @Transactional  //涉及到多张表的怎删改操作需要开启事务
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor -> {
            flavor.setDishId(dish.getId());
        });
        log.info(String.valueOf(flavors.size()));
        if (flavors == null || flavors.size() == 0 ) {
            return;
        }
        dishFlavorMapper.insertBatch(flavors);

    }

    /**
     * 分页查询菜品列表
     * 
 * @param dishPageQueryDTO
     * @return 
     * @author heiheiheng
     * @create 2025/7/18
     **/
    @Override
    public PageResult pageDishQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page  = dishMapper.pageDishQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除菜品
     * 
 * @param dishId
     * @return 
     * @author heiheiheng
     * @create 2025/7/18
     **/
    @Override
    @Transactional
    public void delete(List<Long> dishId) {
        //先判断菜品 是否起售
        dishId.forEach(dishId1 -> {
           Dish dish =  dishMapper.selectById(dishId1);
           if (dish != null && dish.getStatus() == StatusConstant.ENABLE) {
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
           }
        });
        //判断菜品是否被套餐关联
        Integer count = setmealDishMapper.countByDisnId(dishId);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品基本信息dish表
        dishMapper.deleteBatch(dishId);
        //删除菜品口味列表信息dish_flavor
        dishFlavorMapper.deleteBatch(dishId);
    }

    /**
     * 根据id查询菜品
     * 
 * @param id
     * @return 
     * @author heiheiheng
     * @create 2025/7/18
     **/
    @Override
    public DishVO getById(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish, dishVO);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(id);
        dishVO.setFlavors(dishFlavors);
        //构造DishVO对象并返回
        return dishVO;
    }

    /**
     * 修改菜品
     * 
 * @param dishDTO
     * @return 
     * @author heiheiheng
     * @create 2025/7/18
     **/
    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        //修改菜品的基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);
        //修改口味列表信息，dish_flavor
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dish.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public ArrayList<Dish> getDishByCategoryId(Long categoryId) {
        return dishMapper.getDishByCategoryId(categoryId,null);
    }

    @Override
    public void dishStartOrStop(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);
    }

    /**
     * 根据菜品分类和起售状态查询菜品
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        //根据分类id获取菜品
        ArrayList<Dish> dishes = dishMapper.getByCategoryId(dish.getCategoryId(), dish.getStatus());
        ArrayList<DishVO> dishVOS = new ArrayList<>();
        for (int i = 0; i < dishes.size(); i++) {
            //构建DishVO
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dishes.get(i), dishVO);
            //根据菜品id获取菜品口味
            ArrayList<DishFlavor> flavorById = dishMapper.getFlavorById(dishes.get(i).getId());
            dishVO.setFlavors(flavorById);
            dishVOS.add(dishVO);
        }
        return dishVOS;
    }
}
